package elven.codingit.easysearch.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import elven.codingit.easysearch.model.entity.word.Word;

public class IndexWordHelper {

	private static final String replaceGuillemet = "[《》]";
	
	/** 查询的fields字段 **/
	private static String[] savedFields = null;
	private static String[] selectFields = null;
	/**
	 * 给出查询的字段，默认20个
	 * @author liuyy
	 * @date 2016年8月30日
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String[] getSavedKeys(){
		if(savedFields != null){
			return savedFields;
		}
		List fields = new ArrayList<>();
		fields.add("id");
		fields.add("content");
		for(int i = 0; i < 10; i++){
			fields.add("analyze"+i);
		}
		savedFields = (String[]) fields.toArray(new String[fields.size()]);
		return savedFields;
	}
	
	public static String[] getSearchKeys(){
		if(selectFields != null){
			return selectFields;
		}
		/*selectFields = new String[]{"analyze", "content"};*/
		List fields = new ArrayList<>();
		fields.add("content");
		for(int i = 0; i < 10; i++){
			fields.add("analyze"+i);
		}
		selectFields = (String[]) fields.toArray(new String[fields.size()]);
		return selectFields;
	}
	
	/*public static List<Map<String, Object>> writeIndexCont(List<String> contents, List<IndexWord> writes){
		List<Map<String, Object>> writeIndex = new ArrayList<>();
		Map<String, Object> index = null;
		int count = 0;
		for(String key : contents){
			count = 0;
			index = new HashMap<>();
			index.put("id", key);
			for(int j = 0; j < writes.size(); j++){
				IndexWord indexword =  writes.get(j);
				if(indexword.getContent().equalsIgnoreCase(key)){
					index.put("A"+count+"Id", indexword.getId());
					index.put("A"+count+"SearchAnalyze", indexword.getSearchAnalyze());
					index.put("A"+count+"Analyze", indexword.getAnalyze());
					count ++;
				}
			}
			writeIndex.add(index);
		}
		return writeIndex;
	}*/
	
	/**
	 * 合并单词释义
	 * @param word
	 * @author liuyy
	 * @date 2016年8月30日
	 */
	public static Map<String, Object> mergeAnalyze(Map<String, Object> map){
		String analyze = StringUtils.EMPTY;
		Map<String, Object> data = new HashMap<>();
		data.put("id", map.get("id"));
		for(int i = 0; i < 10; i++){
			Object analyzedata = map.get("analyze"+i);
			if(analyzedata != null){
				analyze += analyzedata+"；";
			}
		}
		analyze = analyze.substring(0, analyze.lastIndexOf("；"));
		data.put("analyze", analyze.replaceAll(replaceGuillemet, ""));
		data.put("content", map.get("content"));
		return data;
	}
	
	/**
	 * 根据单词，将结果从数据集中获取出来
	 * @param content 
	 * @param words
	 * @author liuyy
	 * @date 2016年9月7日
	 */
	public static Map<String, Object> getIndexWordByDatas(String content, List<Word> words){
		Map<String, Object> param;
		Map<String, String> analyzeReplace = new HashMap<>();
		int i = 0;
		param = new HashMap<>();
		param.put("content", content);
		try {
			for(Word word : words){
				//如果说是不相等,继续
				if(!word.getContent().trim().equals(content.trim())){
					continue;
				}
				if(i != 0){
					int j = 0;
					for(String key : analyzeReplace.keySet()){
						if(analyzeReplace.get(key).equalsIgnoreCase(word.getmAnalyze())){
							j++;
						}
					}
					if(j == 0){
						analyzeReplace.put("analyze"+i, word.getmAnalyze());
					}
				}else{
					//只有在0条的时候，添加id，第一个释义
					param.put("id", word.getId());
					analyzeReplace.put("analyze0", word.getmAnalyze());
				}
				i++;
			}
			param.putAll(analyzeReplace);
		} catch (Exception e) {
			throw e;
		}
		return param;
	}
}
